
//给定一个二叉树，返回其节点值自底向上的层序遍历。 （即按从叶子节点所在层到根节点所在的层，逐层从左向右遍历） 
//
// 例如： 
//给定二叉树 [3,9,20,null,null,15,7], 
//
// 
//    3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回其自底向上的层序遍历为： 
//
// 
//[
//  [15,7],
//  [9,20],
//  [3]
//]
// 
// Related Topics 树 广度优先搜索 二叉树 
// 👍 468 👎 0

package leetcode.editor.cn;
import leetcode.editor.cn.domain.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
  * 题目Id：107
  * 题目：二叉树的层序遍历 II
  * 日期：2021-08-18 13:36:18
*/
public class BinaryTreeLevelOrderTraversalIi {
    public static void main(String[] args) {
        Solution solution = new BinaryTreeLevelOrderTraversalIi().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> lists = new LinkedList<>();
        if (root==null){
            return lists;
        }
        LinkedList<TreeNode> treeNodes= new LinkedList<>();
        treeNodes.add(root);
        while (!treeNodes.isEmpty()){
            LinkedList<Integer> list = new LinkedList<>();
            int count =treeNodes.size();
            for (int i =0;i<count;i++){
                TreeNode node = treeNodes.pop();
                list.add(node.val);
                if (node.left!=null){
                    treeNodes.add(node.left);
                }
                if (node.right!=null){
                    treeNodes.add(node.right);
                }
            }
            lists.push(list);
        }
        return lists;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}    
